Page({
  data: {
    icon_src:'',
    course:[],
    loadingHidden: false,
    customer_id:0,
    timetable_id:0,
    status_id:-1
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    //加载最新
    this.setData({
      timetable_id: options.timetable_id
    })
    this.requestData(options.timetable_id);
  },

  /**
   * 上拉刷新
   */
  bindscrolltoupper: function () {
    //加载最新
    // this.requestData('newlist');
  },

  /**
   * 加载更多
   */
  bindscrolltolower: function () {
    console.log('到底部')
    //加载更多
    this.requestData('list');
  },

  /**
   * 请求数据
  **/
  requestData: function (timetable_id) {
    if (!getApp().isLogin()){
      this.data.customer_id=170;
    }else{
      this.data.customer_id = getApp().globalData.customer.id;
    }
    var that = this;
    wx.request({
      url: getApp().globalData.ADMIN_API_HOST+'/timetable?1=1',
      data: {
        customer_id: that.data.customer_id,
        timetable_id: timetable_id
      },
      method: 'GET',
      success: function (res) {
        if(res.data.isSuccess){
          that.setData({
            loadingHidden: true,
            course: res.data.data,
            icon_src: getApp().globalData.HOST + res.data.data.icon,
            status_id: res.data.data.status_id
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'loading',
            duration: 2000
          })
        }
      }
    })
  },

  // 预约  
  bookBtn: function () {
    var that = this;
    if (!getApp().isLogin()) {
      that.toLoginPage();
    }
    else {
      that.setData({
        customer_id: getApp().globalData.customer.id
      })
      wx.request({
        url: getApp().globalData.ADMIN_API_HOST+"/book",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        data: {
          customer_id: that.data.customer_id, 
          timetable_id: that.data.timetable_id
        },
        fail: function () {
          wx.showToast({
            title: '网络请求失败',
            icon: 'loading',
            duration: 2000
          })
        },
        success: function (res) {
          that.setData({
            loadingHidden: true,
          });
          that.requestData(that.data.timetable_id);
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel:false,
          })
        },
      })
    }
  },

  // 排队  
  inqueueBtn: function () {
    var that = this;
    if (!getApp().isLogin()) {
      that.toLoginPage();
    }
    else {
      that.setData({
        customer_id: getApp().globalData.customer.id
      })
      wx.request({
        url: getApp().globalData.ADMIN_API_HOST+"/inqueue?",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        data: { 
          customer_id: that.data.customer_id, 
          timetable_id: that.data.timetable_id 
        },
        fail: function () {
          wx.showToast({
            title: '网络请求失败',
            icon: 'loading',
            duration: 2000
          })
        },
        success: function (res) {
          that.setData({
            loadingHidden: true,
          });
          that.requestData(that.data.timetable_id);
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
          })
        },
      })
    }
  },

  // 取消预约
  cancelBtn: function () {
    var that = this;
    if (!getApp().isLogin()) {
      that.toLoginPage();
    }
    else {
      that.setData({
        customer_id: getApp().globalData.customer.id
      })
      wx.request({
        url: getApp().globalData.ADMIN_API_HOST+"/cancel",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        data: {
          customer_id: that.data.customer_id, 
          timetable_id: that.data.timetable_id 
        },
        fail: function () {
          wx.showToast({
            title: '网络请求失败',
            icon: 'loading',
            duration: 2000
          })
        },
        success: function (res) {
          if(res.data.isSuccess){
            that.requestData(that.data.timetable_id);
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              cancelText: '任性取消',
              cancelColor: '#ff0000',
              confirmText: '不取消了',
              success: function (res) {
                if (res.cancel) {
                  that.forceCancel();
                }
              }
            })
          }
        },
      })
    }
  },

  forceCancel:function(){
    var that = this;
    if (!getApp().isLogin()) {
      that.toLoginPage();
    }
    else {
      that.setData({
        customer_id: getApp().globalData.customer.id
      })
      wx.request({
        url: getApp().globalData.ADMIN_API_HOST + "/forceCancel",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        data: {
          customer_id: that.data.customer_id,
          timetable_id: that.data.timetable_id
        },
        fail: function () {
          wx.showToast({
            title: '网络请求失败',
            icon: 'loading',
            duration: 2000
          })
        },
        success: function (res) {
          that.setData({
            loadingHidden: true,
          });
          that.requestData(that.data.timetable_id);
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
          })
        },
      })
    }
  },

  toLoginPage:function(){
    wx.navigateTo({
      url: '../login/login',
    })
  },


  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})